package com.ruoyi.ucar.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ucar.boreq.RechargeRecordDetailQueryBoReq;
import com.ruoyi.ucar.boreq.RechargeRecordListQueryBoReq;
import com.ruoyi.ucar.boresp.*;
import com.ruoyi.ucar.domain.po.RechargeRecordDetailQueryPo;
import com.ruoyi.ucar.domain.po.RechargeRecordListQueryPo;
import com.ruoyi.ucar.mapper.TRechargeRecordMapper;
import com.ruoyi.ucar.moreq.RechargeRecordDetailQueryMoReq;
import com.ruoyi.ucar.moreq.RechargeRecordListQueryMoReq;
import com.ruoyi.ucar.service.ITRechargeRecordService;
import com.ruoyi.ucar.domain.TRechargeRecord;
import com.ruoyi.ucar.util.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 充值记录Service业务层处理
 * 
 * @author sungangbin
 * @date 2025-05-13
 */
@Service
public class TRechargeRecordServiceImpl extends ServiceImpl<TRechargeRecordMapper, TRechargeRecord> implements ITRechargeRecordService
{
    @Autowired
    private TRechargeRecordMapper tRechargeRecordMapper;

    /**
     * 查询充值记录
     * 
     * @param id 充值记录主键
     * @return 充值记录
     */
    @Override
    public TRechargeRecord selectTRechargeRecordById(Long id)
    {
        return tRechargeRecordMapper.selectTRechargeRecordById(id);
    }

    /**
     * 查询充值记录列表
     * 
     * @param tRechargeRecord 充值记录
     * @return 充值记录
     */
    @Override
    public List<TRechargeRecord> selectTRechargeRecordList(TRechargeRecord tRechargeRecord)
    {
        return tRechargeRecordMapper.selectTRechargeRecordList(tRechargeRecord);
    }

    /**
     * 新增充值记录
     * 
     * @param tRechargeRecord 充值记录
     * @return 结果
     */
    @Override
    public int insertTRechargeRecord(TRechargeRecord tRechargeRecord)
    {
        return tRechargeRecordMapper.insertTRechargeRecord(tRechargeRecord);
    }

    /**
     * 修改充值记录
     * 
     * @param tRechargeRecord 充值记录
     * @return 结果
     */
    @Override
    public int updateTRechargeRecord(TRechargeRecord tRechargeRecord)
    {
        return tRechargeRecordMapper.updateTRechargeRecord(tRechargeRecord);
    }

    /**
     * 批量删除充值记录
     * 
     * @param ids 需要删除的充值记录主键
     * @return 结果
     */
    @Override
    public int deleteTRechargeRecordByIds(Long[] ids)
    {
        return tRechargeRecordMapper.deleteTRechargeRecordByIds(ids);
    }

    /**
     * 删除充值记录信息
     * 
     * @param id 充值记录主键
     * @return 结果
     */
    @Override
    public int deleteTRechargeRecordById(Long id)
    {
        return tRechargeRecordMapper.deleteTRechargeRecordById(id);
    }
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public RechargeRecordListQueryBoResp queryRechargeRecordList(RechargeRecordListQueryBoReq rechargeRecordListQueryBoReq) {
        RechargeRecordListQueryBoResp rechargeRecordListQueryBoResp = new RechargeRecordListQueryBoResp();
        Page<RechargeRecordListQueryPo> page=new Page<>(rechargeRecordListQueryBoReq.getPageIndex(),rechargeRecordListQueryBoReq.getPageSize());
        RechargeRecordListQueryMoReq rechargeRecordListQueryMoReq=CommonUtil.objToObj(rechargeRecordListQueryBoReq,RechargeRecordListQueryMoReq.class);
        rechargeRecordListQueryMoReq.setDeleteFlag(1l);
        IPage<RechargeRecordListQueryPo> rechargeRecordListQueryPoIPage= tRechargeRecordMapper.rechargeRecordListQueryPage(page,rechargeRecordListQueryMoReq);
        if(StringUtils.isNotNull(rechargeRecordListQueryPoIPage.getRecords())&&rechargeRecordListQueryPoIPage.getRecords().size()>0){
            rechargeRecordListQueryBoResp.setTotalCount(new Integer(String.valueOf(rechargeRecordListQueryPoIPage.getTotal())));
            rechargeRecordListQueryBoResp.setTotalPage(new Integer(String.valueOf(rechargeRecordListQueryPoIPage.getPages())));
            List<RechargeRecordListQueryBo>  rechargeRecordListQueryBos=new ArrayList<>();
            rechargeRecordListQueryPoIPage.getRecords().forEach(rechargeRecordListQueryPo -> {
                RechargeRecordListQueryBo rechargeRecordListQueryBo=CommonUtil.objToObj(rechargeRecordListQueryPo,RechargeRecordListQueryBo.class);
                rechargeRecordListQueryBo.setRechargeStatusDict(DictUtils.getDictLabel("recharge_status",rechargeRecordListQueryPo.getRechargeStatus()));
                rechargeRecordListQueryBo.setRechargeTypeDict(DictUtils.getDictLabel("recharge_type",rechargeRecordListQueryPo.getRechargeType()));
                rechargeRecordListQueryBos.add(rechargeRecordListQueryBo);
            });
            rechargeRecordListQueryBoResp.setRechargeRecordListQueryBos(rechargeRecordListQueryBos);
            logger.info("充值记录查询BO端返回结果：{}"+rechargeRecordListQueryBoResp);
        }else{
            return null;
        }
        return rechargeRecordListQueryBoResp;
    }

    @Override
    public RechargeRecordDetailQueryBoResp rechargeRecordDetail(RechargeRecordDetailQueryBoReq rechargeRecordDetailQueryBoReq) {
        RechargeRecordDetailQueryMoReq rechargeRecordDetailQueryMoReq=CommonUtil.objToObj(rechargeRecordDetailQueryBoReq, RechargeRecordDetailQueryMoReq.class)  ;
        rechargeRecordDetailQueryMoReq.setDeleteFlag(1l);
        List<RechargeRecordDetailQueryPo> rechargeRecordDetailQueryPos=tRechargeRecordMapper.rechargeRecordDetailQuery(rechargeRecordDetailQueryMoReq);
        if(StringUtils.isNotNull(rechargeRecordDetailQueryPos)&&rechargeRecordDetailQueryPos.size()>0){
            RechargeRecordDetailQueryPo rechargeRecordDetailQueryPo=rechargeRecordDetailQueryPos.get(0);
            RechargeRecordDetailQueryBoResp rechargeRecordDetailQueryBoResp=CommonUtil.objToObj(rechargeRecordDetailQueryPo,RechargeRecordDetailQueryBoResp.class);
            rechargeRecordDetailQueryBoResp.setRechargeStatusDict(DictUtils.getDictLabel("recharge_status",rechargeRecordDetailQueryPo.getRechargeStatus()));
            rechargeRecordDetailQueryBoResp.setRechargeTypeDict(DictUtils.getDictLabel("recharge_type",rechargeRecordDetailQueryPo.getRechargeType()));
            return rechargeRecordDetailQueryBoResp;
        }else{
            return null;
        }
    }
}
